package event

type saveDB struct {
	*SimpleProcessor
	mgr *eventManager
}

func (p *saveDB) Process(e *Event) {
	p.mgr.add(e)
	e.AddListener(func(e *Event) {
		if e.Done() {
			p.mgr.save(e)
		}
	})
}

var saveDBProcessor *saveDB

func init() {
	registerDefaultPlugin("SaveDB", func() EventProcessor {
		if saveDBProcessor == nil {
			mgr, err := GetGlobalEventManager()
			if err != nil {
				panic(err)
			}
			saveDBProcessor = &saveDB{
				SimpleProcessor: NewSimpleMiddleware("SaveDB", nil),
				mgr:             mgr,
			}
		}
		return saveDBProcessor
	})
}
